package com.monodaipoly.perfil;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Registro extends Activity{
	BDglobal bdglobal;
	private EditText mNick;
	private EditText mContraseña;
	private DatePicker mFecha;
	private int mDia;
	private int mMes;
	private int mAno;
	private String mDate;
	//variable de la notificacion
	String ns = Context.NOTIFICATION_SERVICE;
	private static final int NOTIFICATION_ID = 1;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registro);
		bdglobal=((BDglobal) getApplicationContext());		
	}
	
	//Para la notificacion
	private void triggerNotification(){
		 
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.lo2, "¡Nuevo mensaje!", System.currentTimeMillis());
 
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
        contentView.setImageViewResource(R.id.img_notification, R.drawable.lo2);
        contentView.setTextViewText(R.id.txt_notification, "Monodaipoly");
 
        notification.flags |=Notification.FLAG_AUTO_CANCEL;
        
        notification.contentView = contentView;
 
        Intent notificationIntent = new Intent(this, MonoDaiPolyActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
 
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
	
	
	//Guardaremos en la base de datos en usuario
	public void onClickGuardarUsuario(View view){
		
		mNick= (EditText) findViewById(R.id.editText1);
		mContraseña= (EditText) findViewById(R.id.editText2);
		mFecha=(DatePicker) findViewById(R.id.datePicker1);
		mDia=mFecha.getDayOfMonth();
		mMes=mFecha.getMonth()+1;
		mAno=mFecha.getYear();
		mDate=String.valueOf(mDia)+"/"+String.valueOf(mMes)+"/"+String.valueOf(mAno);
		
		//Toast.makeText(getApplicationContext(), mDate, Toast.LENGTH_LONG).show();
		if(mNick.getText().toString().trim().length()>0 && mContraseña.getText().toString().trim().length()>0){
			//int numero = mDbHelper.comprobarNick(mNick.getText().toString());
			Cursor c=bdglobal.getBaseDatosBD().comprobarNick(mNick.getText().toString());
			
			if(c.getCount()>0){
				Toast.makeText(getApplicationContext(), "Ya existe ese nick", Toast.LENGTH_LONG).show();
			
			}if(c.getCount()==0){
				Toast.makeText(getApplicationContext(), "Correcto al registrar usuario", Toast.LENGTH_LONG).show();
				bdglobal.getBaseDatosBD().createUsuario(mNick.getText().toString(), mContraseña.getText().toString(),mDate,null);
				//lo q tarda en aparecer la notificacion status
				Timer timer = new Timer();
                TimerTask timerTask = new TimerTask()
                {
                    @Override
                    public void run()
                    {
                        triggerNotification();
                    }
                };
                timer.schedule(timerTask, 3000);
				
				
				finish();
			}
		}else{
			Toast.makeText(getApplicationContext(), "No dejes campos en blanco ni metas espacios", Toast.LENGTH_LONG).show();
		}	
		
	}
	

}
